// 定义组件
define(function (require,exports,module) {

    var rem=require('charity/js/config/rem'),//rem字体
        details = {
            // 组件
            components : {
                'v-loading':require('charity/js/components/loading.js')  // 加载中组件
            },
            // 首屏初始化数据
            data : {
            	goods_id: '', //物品id
				priceMin:'',//当前最低价格
				priceSubmit:'',//需要提交的价格
                priceAddMin:10,//最低加价幅度
				state:0,//拍卖状态，1结束 0未结束
				//活动
				action: {},
				//商品
				goods:{},
				//竞价记录
				biddings: [],
				record:[//记录
					{
						num:'3043',
						state:'成交',
						time:'10.11 09:33:20',
						price:'2000'
					},
                    {
                        num:'3022',
                        state:'出局',
                        time:'10.11 08:33:20',
                        price:'1500'
                    }
				],
				//详情地址
				actionUrl: global.cxt+'/charity/action/get',
				goodsUrl: global.cxt+'/charity/goods/getGoodsWithBiddingMax',
				biddingListUrl: global.cxt+'/charity/bidding/list',
				buyUrl: global.cxt+'/charity/bidding/save',
				reduceDisabled:true,//禁止降价
                showLoading:true//加载中
            },
            // 事件
            methods : {
            	// 加价
				add:function () {
					var that=this;
                    //that.reduceDisabled=parseInt(that.priceSubmit)>=that.priceMax?false:true;
                    that.priceSubmit=parseInt(that.priceSubmit)+that.priceAddMin;
                    that.reduceDisabled=parseInt(that.priceSubmit)>that.priceMin?false:true;
                },
				// 减价
                reduce:function () {
                    var that=this;
                    that.reduceDisabled=parseInt(that.priceSubmit)>that.priceMin?false:true;
                    if(that.reduceDisabled){
						return
					}else{
                        that.priceSubmit=parseInt(that.priceSubmit)-that.priceAddMin;
                        that.reduceDisabled=parseInt(that.priceSubmit)>that.priceMin?false:true;
					}
                },
				// 限制输入数字
                iptNum: function() {
					var that=this;
					that.priceSubmit=that.priceSubmit.replace(/[^\d]/g, "");
                },
				// 须为整数倍
				tip:function () {
                    /*this.reduceDisabled=parseInt(this.priceSubmit)>this.priceMax?false:true;
                    if((parseInt(this.priceSubmit)-this.priceMax)%this.priceAddMin!=0){
                        this.$messagebox({title:'温馨提示',message:'请输入加价幅度的整数倍！',showCancelButton: true});
                    }else if(parseInt(this.priceSubmit)<=this.priceMax){
                        this.$messagebox({title:'温馨提示',message:'当前价格不能低于最高价格！',showCancelButton: true});
					}*/
                },
				// 购买
				buy:function () {
					var that=this;
					/*if((parseInt(that.priceSubmit)-that.priceMax)%that.priceAddMin!=0){
                        that.$messagebox({title:'温馨提示',message:'请输入加价幅度的整数倍！',showCancelButton: true});
                        return;
					}else if(this.priceSubmit<=this.priceMax){
                        this.$messagebox({title:'温馨提示',message:'当前价格不能低于最高价格！',showCancelButton: true});
                        return;
                    }*/
					if(that.priceSubmit < that.priceMin){
                        that.$messagebox({title:'温馨提示',message:'当前价格不能低于'+that.priceMin+'！',showCancelButton: true});
                        return;
					}
                    // 显示loading
                    that.$indicator.open({
                        spinnerType: 'fading-circle'
                    });
                    // 请求登录
                    $.ajax({
                        url:that.buyUrl,
                        type:'post',
                        dataType:'json',
                        data: {
                        	goods_id: that.goods_id,
                        	price: that.priceSubmit
                        },
                        success:function (res) {
                            // 关闭loading
                            that.$indicator.close();
                            // 登录成功
                            if(res.code=='0'){
                                that.$messagebox({title:'温馨提示',message:res.msg,showCancelButton: true});
                                setTimeout(function () {
                                    window.location.reload();
                                },2000)
                            }else{
                            	that.$messagebox({title:'温馨提示',message:res.msg,showCancelButton: true});
                            }
                        }
                    })
                },
                //详情
                loadData: function() {
                	var that=this;
                	that.goods_id = location.href.split('details?id=')[1];
                	//活动信息
                    $.ajax({
                        url: that.actionUrl,
                        type:'post',
                        dataType:'json',
                        success:function (res) {
                            // 关闭loading
                        	that.$indicator.close();
                        	if(res.code=='111111') {
                        		window.location.href = res.data;
                        		return;
                        	}
                            
                            if(res.code=='0'){
                            	res.data.start_time = res.data.start_time.parseDate();
                            	res.data.end_time = res.data.end_time.parseDate();
                            	var d = new Date();
                            	//为开始
                            	if(d.getTime()<res.data.start_time.getTime())
                            		res.data.state = 'stateWait';
                            	//进行中
                            	if(d.getTime()>=res.data.start_time.getTime() && d.getTime()<=res.data.end_time.getTime())
                            		res.data.state = 'stateBe';
                            	//已结束
                            	if(d.getTime() > res.data.end_time.getTime())
                            		res.data.state = 'stateStop';
                            	that.action=res.data;
                            }else{
                            	that.$messagebox({title:'温馨提示',message:res.msg,showCancelButton: true})
                            }
                        }
                    })
                    
                    $.when(
	                    	//物品详情
	                    	$.ajax({
		                        url:that.goodsUrl,
		                        type:'post',
		                        dataType:'json',
		                        data: {id: that.goods_id}
	                    	}),
	                    	//竞价请求
		                    $.ajax({
		                        url:that.biddingListUrl,
		                        type:'post',
		                        dataType:'json',
		                        data: {goods_id: that.goods_id, page_size: 999999}
		                    })
                    ).done(function(res1, res2) {
                        that.$indicator.close();
                        
                        if(res1[0].code=='0'){
                        	res1[0].data.image = global.cxt+'/view/charity/images/temp/' + res1[0].data.image;
                            that.goods = res1[0].data;
                            that.priceAddMin = that.goods.increase;
                        }else{
                            // 错误提示
                        }
                        
                        //竞价
                        if(res2[0].code=='0'){
                            that.biddings = res2[0].data.list;
                        }else{
                            // 错误提示
                        }
                        
                        that.priceMin = that.biddings.length>0? 
                        		that.biddings[0].price+that.goods.increase: 
                            	that.goods.price+that.goods.increase;
                        
                        that.priceSubmit = that.priceMin;
                    })
                    /*
                	//请求详情
                	$.ajax({
                        url:that.goodsUrl,
                        type:'post',
                        dataType:'json',
                        data: {id: id},
                        success:function (res) {
                            // 关闭loading
                            that.$indicator.close();
                            // 登录成功
                            if(res.code=='0'){
                            	res.data.image = global.cxt+'/view/charity/images/temp/' + res.data.image;
                                that.goods = res.data;
                            }else{
                                // 错误提示
                            }
                        }
                    })
                    //竞价请求
                    $.ajax({
                        url:that.biddingListUrl,
                        type:'post',
                        dataType:'json',
                        data: {goods_id: id, page_size: 999999},
                        success:function (res) {
                            // 关闭loading
                            that.$indicator.close();
                            // 登录成功
                            if(res.code=='0'){
                                that.biddings = res.data.list;
                            }else{
                                // 错误提示
                            }
                        }
                    })
                    */
                }
            }
        };
    
	var html = require('charity/templates/details.tpl'),
	    index = Vue.extend({
	      	template:html,
		    data:function(){
				return details.data
		    },
		    props:[],
	        components:details.components,
	        beforeCreate:function(){
	        	require('charity/css/details.css');
                require('charity/css/components/e-ui-css.css');
                require('charity/js/lib/mint-ui');
	        },
		    // 实例被创建之后被调用
		    created:function(){
		    	this.loadData();
		    },
			computed:{},
            mounted:function(){
                this.showLoading = false;
                //this.priceSubmit=this.priceMax;
			},
		    methods:details.methods
		});
	module.exports = index;

});

